package com.example.orderapplication.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.example.orderapplication.R
import com.example.orderapplication.bean.DataX

/**
 * @author: Xu QiHao
 * @date: 2021/10/2213:52
 * @Description：
 */
class SearchAdapter(context: Context, list: List<DataX>?) : BaseAdapter() {
    private val context: Context = context
    private val data: List<DataX>? = list
    override fun getCount(): Int {
        return data?.size ?: 10
    }

    override fun getItem(position: Int): Any {
        return data!!.size
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var viewHold: ViewHold? = null
        var v: View? = null
        val subcategory: DataX = data!![position]
        if (convertView == null) {
            viewHold = ViewHold()
            v = View.inflate(context, R.layout.item_search_category, null)
            viewHold.mItemAlbum = v.findViewById(R.id.item_album)
            viewHold.mItemHomeName = v.findViewById(R.id.item_home_name)
            viewHold.mTvPrice = v.findViewById(R.id.tv_price)
            v.tag = viewHold
        } else {
            v = convertView
            viewHold = v.tag as ViewHold
        }
        viewHold.mItemAlbum?.let {
            Glide.with(context)
                .load(subcategory.imgURL)
                .placeholder(R.drawable.ic_launcher_background)
                .into(it)
        }
        viewHold.mItemHomeName?.text = subcategory.title
        viewHold.mTvPrice?.text = if (subcategory.desc!!.isEmpty()) {
            "暂无"
        } else {
            subcategory.desc
        }

        return v!!
    }

    inner class ViewHold {
        var mItemAlbum: ImageView? = null
        var mItemHomeName: TextView? = null
        var mTvPrice: TextView? = null

    }
}